<div id="dialog-addUserPreferServer" class="form-horizontal">
	<div class="modal-body">
	   <div class="form-group">
	       <label class="col-sm-3 control-label">服务器类型</label>
	       <div class="col-sm-9">
	           <select class="form-control" name="serverType" onchange="doSelectServerType();" style="width:100%">   
			            <option value="mxsdoc">MXSDOC</option>  
			        	<option value="ftp">FTP</option>   
			            <option value="sftp">SFTP</option>   
			            <option value="smb">SMB</option>  
			            <option value="svn">SVN</option>  
			            <option value="git">GIT</option>  
			   </select>
	       </div>
	       <div class="clearfix"></div>
	   </div>
	   <div class="form-group">
	       <label class="col-sm-3 control-label">服务器地址</label>
	       <div class="col-sm-9">
 	           <input class="form-control" type="text" name="serverUrl" placeholder="请填写您的服务器地址" style="width: 100%"/>
	       </div>
	       <div class="clearfix"></div>
	   </div>
	   <div class="form-group">
	       <label class="col-sm-3 control-label">账号</label>
	       <div class="col-sm-9">
 	           <input class="form-control" type="text" name="userName" style="width: 100%"/>
	       </div>
	       <div class="clearfix"></div>
	   </div>
	   <div class="form-group">
	    	<label class="col-sm-3 control-label">密码</label>
	        <div class="col-sm-9">
	            <input class="form-control"  type="password" name="pwd" maxlength="16"></input>
	        </div>
			<div class="clearfix"></div>
	   </div>
	   <div class="form-group" style="display: none;">
	       <label class="col-sm-3 control-label">字符集</label>
	       <div class="col-sm-9">
	           <select class="form-control" name="charset" style="width:100%">   
			            <option value="utf-8">UTF-8</option>  
			        	<option value="gbk">GBK</option>   
			            <option value="gb2312">GB2312</option>   
			            <option value="big5">BIG5</option>  
			   </select>
	       </div>
	       <div class="clearfix"></div>
	   </div> 
	   <div class="form-group" style="display: none;">
	       <label class="col-sm-3 control-label">被动模式</label>
	       <div class="col-sm-9">
	           <select class="form-control" name="passiveMode" style="width:100%">   
			        	<option value="0">否</option>   
			            <option value="1">是</option>  
				</select>
	       </div>
	       <div class="clearfix"></div> 
		</div>
    <div class="modal-footer">
	   <div class="form-group">
	       <label class="col-sm-3 control-label">服务器名称</label>
	       <div class="col-sm-9">
 	           <input class="form-control" type="text" name="serverName" placeholder="自定义服务器名称" style="width: 100%"/>
	       </div>
	       <div class="clearfix"></div>
	   </div>
	</div>
    <div class="modal-footer">
	    <div class="form-group">
		    <div class="col-sm-12 text-right">
		        <button  type="button" class="btn btnPrimary" onclick="doAddUserPreferServer();" style="width: 100px;height: 42px;">确定</button>
		        <button  type="button" class="btn btnPrimary" onclick="cancelAddUserPreferServer();" style="width: 100px;height: 42px;">取消</button>
			</div>
		</div>
	</div>
</div>

<script src="js/base64.js"></script>
<script type="text/javascript">
	function addUserPreferServerPageInit(_callback, _serverInfo)
	{
		console.log("addUserPreferServerPageInit(): _serverInfo", _serverInfo);
		
		AddUerPreferServer.init(_callback, _serverInfo);
	}
	
	function doSelectServerType()
	{		
		var serverType = $("#dialog-addUserPreferServer select[name='serverType']").val();
		console.log("doSelectServerType serverType:", serverType);
		//选择ftp时显示字符集和被动模式
		if(serverType == "ftp")
		{
			$("#dialog-addUserPreferServer select[name='charset']").parent().parent().show();
			$("#dialog-addUserPreferServer select[name='passiveMode']").parent().parent().show();
		}
		else
		{
			$("#dialog-addUserPreferServer select[name='charset']").parent().parent().hide();
			$("#dialog-addUserPreferServer select[name='passiveMode']").parent().parent().hide();
		}
	}

	function closeAddUserPreferServerDialog()
	{
		closeBootstrapDialog("addUserPreferServer");
	}

	function doAddUserPreferServer()
	{
		AddUerPreferServer.doAddUserPreferServer();
	}
	
	function cancelAddUserPreferServer()
	{
		closeAddUserPreferServerDialog();
	}
	
	var AddUerPreferServer = (function () {
		var callback;
		var serverInfo;
		var serverUrl;
		var serverName;
		var userName;
		var pwd;
		var params = "";

		function init(_callback, _serverInfo)
		{
			console.log("AddUerPreferServer.init() _serverInfo:", _serverInfo);
			serverInfo = _serverInfo;
			callback = _callback;						
		}
		
		function doAddUserPreferServer()
		{
			serverType = $("#dialog-addUserPreferServer select[name='serverType']").val();
			serverUrl = $("#dialog-addUserPreferServer input[name='serverUrl']").val();
			serverName = $("#dialog-addUserPreferServer input[name='serverName']").val();
			userName = $("#dialog-addUserPreferServer input[name='userName']").val();
		    pwd = base64_encode($("#dialog-addUserPreferServer input[name='pwd']").val());
			
		    switch(serverType)
		   	{
		    case "ftp":
		    	var charset = $("#dialog-addUserPreferServer select[name='charset']").val();
				var passiveMode = $("#dialog-addUserPreferServer select[name='passiveMode']").val();
				if(charset != "")
				{
					params += "charset=" + charset + ";";
				}
				if(passiveMode != "")
				{
					params += "isPassive=" + passiveMode + ";";
				}
		    	break;
		   	}
		    
		    if(serverUrl)
			{
		  	  	addUserPreferServer(serverType, serverName, serverUrl, userName, pwd, params);
		      	return true;
			}
			else
			{
		    	alert("服务器地址不能为空");
		        return false;
		    }
		}
		
	   	function addUserPreferServer(serverType, serverName, serverUrl, userName, pwd, params)
	   	{
	    	$.ajax({
	             url : "/DocSystem/Bussiness/addUserPreferServer.do",
	             type : "post",
	             dataType : "json",
	             data : {
					serverType: serverType,
	            	serverName: serverName,
	            	serverUrl: serverUrl,
		            userName: userName,
		            pwd: pwd,
		            params: params,
	             },
	             success : function (ret) {
	            	console.log("addUserPreferServer ret:", ret);            		
	             	if( "ok" == ret.status){             		
	             		// 普通消息提示条
						bootstrapQ.msg({
									msg : "添加成功！",
									type : 'success',
									time : 2000,
						});
	             		
	             		callback && callback();
	            		//closeAddUserPreferServerDialog();
	                }
	                else
	                {
	                	showErrorMessage("添加失败:" + ret.msgInfo);
	                }
	            },
	            error : function () {
	                showErrorMessage("添加失败:服务器异常！");
	            }
	        });
	    }
	   	
		//开放给外部的调用接口
        return {
			init: function(_serverInfo, _callback){
				init(_serverInfo, _callback);
			},
			doAddUserPreferServer: function(){
				doAddUserPreferServer();
            },
        };
	})();
</script>

